package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.About;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.AboutService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/2 19:45
 */
@RestController
@RequestMapping(ManagerVersion.API_V1_PREFIX+"/about")
@Api(value = "关于我们")
public class AboutController {

    @Autowired
    private AboutService aboutService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 关于我们页面
     * @return
     */
    @GetMapping("aboPage")
    public Object aboutPage(){
        return new ModelAndView("about/aboutPage");
    }

    /**
     * 查询关于我们
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @ApiOperation(value ="关于我们列表",notes = "关于我们列表")
    @PostMapping("aboutList")
    public Object selAbout(@RequestParam("limit")Integer pageSize,
                           @RequestParam("page")Integer pageIndex){
        PageInfo<About> info = aboutService.selectByAboutList(new About(),pageIndex,pageSize);
        return new ReturnResult<About>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 修改页面
     * @param id
     * @return
     */
    @GetMapping("eidtabout")
    public Object updateAboutPage(@RequestParam("id")Integer id){
        About about = aboutService.selectByPrimaryKey(id);
        ModelAndView mv = new ModelAndView("about/eidabout");
        mv.addObject("about",about);
        return mv;
    }

    /**
     * 修改关于我们
     * @param about
     * @return
     */
    @ApiOperation(value ="修改关于我们",notes = "修改关于我们")
    @PostMapping("updateAbout")
    public Object editAbout(About about){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改关于我们");
        if(null != about){
            About old = aboutService.selectByPrimaryKey(about.getId());
            about.setUpdateTime(new Date());
            aboutService.updateByAboutKey(about,old);
        }
        return new ModelAndView("about/aboutPage");
    }
}
